package com.bskj.kuaifu.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bskj.common.bean.model.UserInfo;
import com.bskj.common.bean.model.UserThirdAuth;
import com.bskj.common.dao.UserInfoDao;
import com.bskj.common.dao.UserThirdAuthDao;
import com.bskj.common.dao.core.BaseDao;
import com.bskj.common.dao.core.FindRequestEntity;
import com.bskj.common.dao.core.WhereEnum;
import com.bskj.common.enums.ThirdPlatformTypeEnum;
import com.bskj.common.service.impl.BaseServiceImpl;
import com.bskj.common.utils.ResultMapUtil;
import com.bskj.kuaifu.service.IUserThirdAuthService;

@Service
public class UserThirdAuthServiceImpl extends BaseServiceImpl<UserThirdAuth> implements IUserThirdAuthService {
	@Autowired
	private UserThirdAuthDao userThirdAuthDao;

	@Autowired
	private UserInfoDao userInfoDao;

	public BaseDao<UserThirdAuth> getBaseDao() {
		return userThirdAuthDao;
	}

	public UserInfo getUserInfoWithWeInfo(String appid, String openid, ThirdPlatformTypeEnum thirdPlatformType)
			throws Exception {
		FindRequestEntity param = new FindRequestEntity();
		param.addWhere("platform_type", WhereEnum.EQ, thirdPlatformType.getKey());
		param.addWhere("third_platform_id", WhereEnum.EQ, appid);
		param.addWhere("openid", WhereEnum.EQ, openid);

		Integer userId = 0;
		List<UserThirdAuth> userThirdAuthList = userThirdAuthDao.find(param);
		if (userThirdAuthList != null && userThirdAuthList.size() > 0) {
			userId = userThirdAuthList.get(0).getUserId();
		} 

		if (userId != null && userId != 0) {
			return userInfoDao.get(userId);
		}

		return null;
	}

	@Override
	public UserThirdAuth getUserThirdAuth(Integer userId, String appid) throws Exception {
		FindRequestEntity param = new FindRequestEntity();
		param.addWhere("platform_type", WhereEnum.EQ, ThirdPlatformTypeEnum.We_Small_App.getKey());
		param.addWhere("third_platform_id", WhereEnum.EQ, appid);
		param.addWhere("user_id", WhereEnum.EQ, userId);
		List<UserThirdAuth> userThirdAuthList = userThirdAuthDao.find(param);
		if (userThirdAuthList != null && userThirdAuthList.size() > 0) {
			return userThirdAuthList.get(0);
		} 
		return null;
	}

	@SuppressWarnings("null")
	@Override
	public Map<String, Object> saveThirdAuth(String appid, String openid, String unionid, ThirdPlatformTypeEnum type)
			throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		FindRequestEntity param = new FindRequestEntity();
		param.addWhere("unionid", WhereEnum.EQ, unionid);
		List<UserInfo> userList = userInfoDao.find(param);
		UserInfo userInfo = null;
		if(userList != null && userList.size() > 0) {
			userInfo = userList.get(0);
		} else {
			userInfo = new UserInfo();
			userInfo.setUnionid(unionid);
			userInfo.setCreateTime(new Date());
			userInfo.setUpdateTime(new Date());
			userInfoDao.insert(userInfo);
		}
		UserInfo existUserInfo = getUserInfoWithWeInfo(appid, openid, type);
		if(existUserInfo == null) {
			UserThirdAuth userThirdAuth = new UserThirdAuth();
			userThirdAuth.setThirdPlatformId(appid);
			userThirdAuth.setOpenid(openid);
			userThirdAuth.setUserId(userInfo.getId());
			userThirdAuth.setCreateTime(new Date());
			userThirdAuth.setUpdateTime(new Date());
			userThirdAuth.setPlatformType(type.getKey());
			insert(userThirdAuth);
		} 
		resultMap = ResultMapUtil.genSuccessMap("上行小程序用户成功");
		return resultMap;
	}
}
